package com.ujoin.service;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.ujoin.bean.Activity;
import com.ujoin.bean.Collection;
import com.ujoin.bean.User;
import com.ujoin.dao.imp.ActivityDAO;
import com.ujoin.dao.imp.CollectionDAO;
import com.ujoin.dao.imp.UserDAO;
import com.ujoin.util.JsonUtil;

/**
 * 
 * @author Daniel
 * 待测试
 *
 */
public class CollectionService {
	@Autowired
	private CollectionDAO collectionDao;
	
	@Autowired
	private ActivityDAO activityDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	/**
	 * 收藏活动
	 * @param collection
	 * @return Json
	 */
	public String createCollection(Collection collection){
		JsonUtil jo = new JsonUtil();
		Activity activity = activityDAO.findById(collection.getActivityId());
		if (activity == null) {
			jo.getError();
			jo.setMessage("活动不存在，无法收藏");
			return jo.toJSONString();
		}
		User user = userDAO.findById(collection.getUserId());
		if (user == null) {
			jo.getError();
			jo.setMessage("用户不存在，无法收藏");
			return jo.toJSONString();
		}
		if (collectionDao.findByUserIdActivityId(collection.getUserId(), collection.getActivityId())!=null) {
			jo.getError();
			jo.setMessage("收藏已存在，无需再收藏");
			return jo.toJSONString();
		}
		
		collection.setValid(1);
		
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		collection.setCreated(timestamp);
		
		collectionDao.save(collection);
		
		jo.getSuccess();
		jo.setAllComponentsName(collection);
		
		return jo.toJSONString();
	}
	
	/**
	 * 取消收藏（根据collection的userId和activityId）
	 * @param collection
	 * @return Json
	 */
	public String deleteCollection(Collection collection){
		JsonUtil jo = new JsonUtil();
		Collection c = collectionDao.findByUserIdActivityId(collection.getUserId(), collection.getActivityId());
		if ( c == null ) {
			jo.getError();
			jo.setMessage("收藏不存在，无法删除收藏");
			return jo.toJSONString();
		}
		
		collectionDao.delete(c);
		
		jo.getSuccess();
		return jo.toJSONString();
	}
	
	/**
	 * 使收藏无效（根据collectionId）
	 * @param collection
	 * @return Json
	 */
	public String invalidateCollectionById(int collectionId) {
		JsonUtil jo = new JsonUtil();
		Collection collection = collectionDao.findById(collectionId);
		
		if ( collection == null ) {
			jo.getError();
			jo.setMessage("收藏不存在，无法无效化收藏");
			return jo.toJSONString();
		}
		
		collection.setValid(0);
		collectionDao.attachDirty(collection);
		
		jo.getSuccess();
		return jo.toJSONString();
	}
	
	/**
	 * 使收藏无效（根据collection的userId和activityId）
	 * @param collection
	 * @return Json
	 */
	public String invalidateCollectionByUserIdActivityId(int userId, int activityId) {
		JsonUtil jo = new JsonUtil();
		Collection collection = collectionDao.findByUserIdActivityId(userId, activityId);
		
		if ( collection == null ) {
			jo.getError();
			jo.setMessage("收藏不存在，无法无效化收藏");
			return jo.toJSONString();
		}
		
		collection.setValid(0);
		collectionDao.attachDirty(collection);
		
		jo.getSuccess();
		return jo.toJSONString();
	}
	
	/**
	 * 使收藏无效（无效化某个活动的所有收藏）
	 * @param userId
	 * @param activityId
	 * @return Json
	 */
	public String invalidateCollectionByActivityId(int activityId) {
		JsonUtil jo = new JsonUtil();
		List list = collectionDao.findByActivityId(activityId);
		
		if ( list.isEmpty() ) {
			jo.getError();
			jo.setMessage("收藏不存在，无法无效化收藏");
			return jo.toJSONString();
		}
		
		Iterator iterator = list.iterator();
		while ( iterator.hasNext() ) {
			Collection collection = (Collection) iterator.next();
			collection.setValid(0);
			collectionDao.attachDirty(collection);
		}
		
		jo.getSuccess();
		return jo.toJSONString();
	}
	
	/**
	 * 使收藏无效（无效化某个用户的所有收藏）
	 * @param userId
	 * @param activityId
	 * @return Json
	 */
	public String invalidateCollectionByUserId(int userId) {
		JsonUtil jo = new JsonUtil();
		List list = collectionDao.findByUserId(userId);
		
		if ( list.isEmpty() ) {
			jo.getError();
			jo.setMessage("收藏不存在，无法无效化收藏");
			return jo.toJSONString();
		}
		
		Iterator iterator = list.iterator();
		while ( iterator.hasNext() ) {
			Collection collection = (Collection) iterator.next();
			collection.setValid(0);
			collectionDao.attachDirty(collection);
		}
		
		jo.getSuccess();
		return jo.toJSONString();
	}
	
	/**
	 * 找出某个用户的所有收藏
	 * @param collection
	 * @return Json
	 */
	public String findCollectionByUserId(int userId){
		JsonUtil jo = new JsonUtil();
		List list = collectionDao.findByUserId(userId);
		
		jo.getSuccess();
		jo.setComponentList(list);
		
		return jo.toJSONString();
	}
	
	/**
	 * 找出某个活动的所有收藏
	 * @param collection
	 * @return Json
	 */
	public String findCollectionByActivityId(int activityId){
		JsonUtil jo = new JsonUtil();
		List list = collectionDao.findByUserId(activityId);
		
		jo.getSuccess();
		jo.setComponentList(list);
		
		return jo.toJSONString();
	}
	
	/**
	 * 找出所有收藏
	 * @return Json
	 */
	public String findAllCollection(){
		return new JsonUtil().setComponentList(collectionDao.findAll()).setSuccess().toJSONString();
	}
}
